//
//  RemindCell.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/11.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "RemindCell.h"
#import "DataTool.h"
@interface RemindCell()

@end

@implementation RemindCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

+(instancetype)setRemindCellWithTableView:(UITableView *)tableView{
    static NSString *reuseId = @"an";
    RemindCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RemindCell" owner:nil options:nil] lastObject];
    }
    return cell;
}

-(void)setSelcect:(BOOL)selcect{
    
}
-(void)setData:(RemingThings *)data{
    _titleName.text =data.title;
    NSString *createTime =data.createTime;
    NSString *showTime =[DataTool timestampChaStringDate2:[createTime longLongValue]];
    _crreateTime.text =showTime;
    //在这里进行计算
    NSString *doTime =data.doTime;
    NSString *remindtime =[DataTool timestampChaStringDate:[doTime longLongValue]];
    remindtime =[remindtime stringByAppendingString:@", "];
    remindtime =[remindtime stringByAppendingString:data.doCircle];
    if ([data.doCircle isEqualToString:@"一次"]) {
        BOOL isPast =[DataTool dateCompareToNowDate:[DataTool dateFromString:remindtime]];
        if (!isPast) {
            _timeShow.textColor =[UIColor redColor];
            remindtime =[remindtime stringByAppendingString:@", 已过期"];
        }else{
            remindtime =[remindtime stringByAppendingString:@", 未执行"];
        }
    }else if ([data.doCircle isEqualToString:@"每天"]){
        NSDate *doTimeDate =[DataTool timestampChangeToDate:[doTime longLongValue]];
        NSDateComponents* com =[DataTool getDateDifference:[NSDate date] withAnotherDay:doTimeDate by:NSCalendarUnitDay];
        NSInteger day =[com day];
        if (day>0){
            //已执行
            NSString *nss =@", 执行";
            NSString *number =[NSString stringWithFormat:@"%ld",(long)day];
            nss=[nss stringByAppendingString:number];
            nss=[nss stringByAppendingString:@"次"];
           remindtime =[remindtime stringByAppendingString:nss];
        }else if(day==0){
            //判断
            NSInteger minute =[com minute];
            if (minute>=0){
                remindtime =[remindtime stringByAppendingString:@", 执行1次"];
            }else{
                remindtime =[remindtime stringByAppendingString:@", 未执行"];
            }
        }else{
            //未执行
             remindtime =[remindtime stringByAppendingString:@", 未执行"];
        }
    }else if ([data.doCircle isEqualToString:@"每周"]){
        NSDate *doTimeDate =[DataTool timestampChangeToDate:[doTime longLongValue]];
        NSDateComponents* com =[DataTool getDateDifference:[NSDate date] withAnotherDay:doTimeDate by:kCFCalendarUnitWeekday];
        NSInteger week =[com weekday];
        if (week/7>=1){//
            NSString *nss =@", 执行";
            NSString *number =[NSString stringWithFormat:@"%i",(int)week/7];
            nss=[nss stringByAppendingString:number];
            nss=[nss stringByAppendingString:@"次"];
            remindtime =[remindtime stringByAppendingString:nss];
        }else{
            remindtime =[remindtime stringByAppendingString:@", 未执行"];
        }
    }else if ([data.doCircle isEqualToString:@"每两周"]){
        NSDate *doTimeDate =[DataTool timestampChangeToDate:[doTime longLongValue]];
        NSDateComponents* com =[DataTool getDateDifference:[NSDate date] withAnotherDay:doTimeDate by:kCFCalendarUnitWeekday];
        NSInteger week =[com weekday];

        if (week/14>=1){//
            NSString *nss =@", 执行";
            NSString *number =[NSString stringWithFormat:@"%i",(int)week/14];
            nss=[nss stringByAppendingString:number];
            nss=[nss stringByAppendingString:@"次"];
            remindtime =[remindtime stringByAppendingString:nss];
        }else{
            remindtime =[remindtime stringByAppendingString:@", 未执行"];
        }
    }else if ([data.doCircle isEqualToString:@"每月"]){
        NSDate *doTimeDate =[DataTool timestampChangeToDate:[doTime longLongValue]];
        NSDateComponents* com =[DataTool getDateDifference:[NSDate date] withAnotherDay:doTimeDate by:kCFCalendarUnitMonth];
        NSInteger week =[com month];
        NSLog(@"week=====%ld",(long)week);
        if (week>=1){//
            NSString *nss =@", 执行";
            NSString *number =[NSString stringWithFormat:@"%i",(int)week];
            nss=[nss stringByAppendingString:number];
            nss=[nss stringByAppendingString:@"次"];
            remindtime =[remindtime stringByAppendingString:nss];
        }else{
            remindtime =[remindtime stringByAppendingString:@", 未执行"];
        }
    }else if ([data.doCircle isEqualToString:@"每年"]){
        NSDate *doTimeDate =[DataTool timestampChangeToDate:[doTime longLongValue]];
        NSDateComponents* com =[DataTool getDateDifference:[NSDate date] withAnotherDay:doTimeDate by:kCFCalendarUnitYear];
        NSInteger week =[com year];
        NSLog(@"week=====%ld",(long)week);
        if (week>=1){//
            NSString *nss =@", 执行";
            NSString *number =[NSString stringWithFormat:@"%i",(int)week];
            nss=[nss stringByAppendingString:number];
            nss=[nss stringByAppendingString:@"次"];
            remindtime =[remindtime stringByAppendingString:nss];
        }else{
            remindtime =[remindtime stringByAppendingString:@", 未执行"];
        }
    }
    _timeShow.text =remindtime;
    if (data.selcect){
        [_changgeView setImage:[UIImage imageNamed:@"choose_pre"]];
    }else{
        [_changgeView setImage:[UIImage imageNamed:@"choose_nor"]];
    }
}
@end
